﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using Wicresoft.Framework.Organization;
using JDEduTrain.Framework.Business;
using JDEduTrain.Framework.ViewEntity;
using JDEduTrain.Framework.Entity;
using Wicresoft.JDPostBoard.Entity;

public partial class PostBoard_MyPostList : BaseAuthenticationPage
{
    public string SearchDefaultText
    {
        get
        {
            return "搜索 留言标题";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1 and PosterID='" + CurrentUser.ID.ToString() + "' and IsDelete='false' ";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }
    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "searchClick();");
        this.ShowLRLayoutButton = false;
        this.ShowUDLayoutButton = false;
        this.ShowSimpleLayoutButton = false;
        this.ShowAdvancedSearch = false;

        this.Title = "我的留言";
        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "PostTime";
            this.Order.OrderDirection = "DESC";
            BindData();
        }

    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    private void BindData()
    {
        int recordCount = 0;
        DataTable dtPostList = DataAccess.Select(typeof(Post), this.Pager.PageIndex, this.Pager.PageSize, SearchClause, this.Order.OrderColumn + " " + this.Order.OrderDirection, ref recordCount);
        Pager.RecordCount = recordCount;

        this.rptPostList.DataSource = dtPostList;
        rptPostList.DataBind();

    }

    protected void rptPostList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Image imgSign = e.Item.FindControl("imgSign") as Image;
            Guid id = new Guid(((DataRowView)e.Item.DataItem).Row["ID"].ToString());
            Post post = new Post(id);
            //if (post.Load())
            //{
            //    if (post.IsRead)
            //    {
            //        imgSign.ImageUrl = "../Images/Icon/Object/web-readed.GIF";
            //        imgSign.ToolTip = "已读";
            //    }
            //    else
            //    {
            //        imgSign.ImageUrl = "../Images/Icon/Object/web.GIF";
            //        imgSign.ToolTip = "未读";
            //    }
            //}
            //else
            //    imgSign.Visible = false;
         //   imgSign.ImageUrl = "../Images/Icon/Object/Mail.gif";

        }
    }

    protected void rptPostList_OnItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        string pk = e.CommandArgument.ToString();
        if (e.CommandName == "Delete")
        {
            Post post = new Post(new Guid(pk));
            post.Load();
            post.IsDelete = true;
            post.Save();
            WebCommon.ShowMessage(this, "删除成功");
            BindData();
        }
        if (e.CommandName == "Edit")
        {
            WebCommon.OpenWindow(this, "NewPostDetail.aspx?PostID=" + pk + "&Type=Edit&nc=" + new Random().Next(),"newwindow", "width=330,height=300,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,left=' + (screen.width - 330) / 2 + ',top=' + (screen.height - 300) / 2+'");
        }
    }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchClause = "1=1 and PosterID='" + CurrentUser.ID.ToString() + "'  and IsDelete='false' ";

        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause += string.Format(" and {0} LIKE N'%{1}%'", Post.SQLCOL_POSTTITLE, tbSearch.Text);
            
        BindData();
    }
    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        BindData();

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        //ArrayList alClass = new ArrayList();

        //for (int i = 0; i < rptUserInfo.Items.Count; i++)
        //{
        //    if ((rptUserInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
        //    {
        //        Guid id = new Guid(DTUserPageList.Rows[rptUserInfo.Items[i].ItemIndex]["ID"].ToString());
        //        alClass.Add(id);
        //    }
        //}
        //BLTrain.DeleteTrainClass((Guid[])alClass.ToArray(typeof(Guid)));
        //BindData();
    }

}
